layui.use(['table','form', 'laydate'], function() {
    var table = layui.table
        , form = layui.form
        , laydate = layui.laydate
        , $ = layui.$;
    let user = JSON.parse(sessionStorage.getItem('user'));
    //总课表
    table.render({
        elem: '#userTable',
        height: 'full-58',
        url: '/Class/table/getAllTables', //数据接口
        title: '总课表',
        page: true ,//开启分页
        limits:[10,20,30,40],
        where: {"realName": user.realName ? user.realName : '',"year": 0, "time": utils.formatDate(new Date())+'~2021-06-30', "week": $('#week').val()},
        cols:
            [[ //表头
                { field: 'id',title: 'ID',sort: true,hide: true},
                { field: 'courseId',title: '课程'},
                { field: 'classId',title: '班级'},
                { field: 'roomId',title: '教室'},
                { field: 'userId',title: '教师'},
                { field: 'schoolYear',title: '学年', sort: true},
                { field: 'schoolTerm',title: '学期', sort: true},
                { field: 'week',title: '周数'},
                { field: 'weekNum',title: '周几'},
                { field: 'timeSlot',title: '节数'},
                { field: 'state',title: '课表状态', templet: (d) => {
                    if(d.state == 0){
                        return '正常';
                    }else return '调整';
                    }},
                { field: 'createTime',title: '上课时间', templet: (d) =>{
                      return utils.formatDate(d.createTime)
                    }},
            ]]
    });
    //日期范围
    laydate.render({
        elem: '#time'
        ,range: '~',
        value: utils.formatDate(new Date())+' ~ 2021-06-30'
    });
    //触发行双击事件
    table.on('rowDouble(userTable)', function(obj){
        //检查课程能否调整
        $.ajax({
            type: 'post',
            url: '/Class/apply/check',
            dataType: 'json',
            data: { "id": obj.data.id, "type": 0},
            success: (res) =>{
                if(res.code !== 0){
                    layer.msg(res.msg);
                }else{
                    sessionStorage.setItem('courseData', JSON.stringify(obj.data)); //传递行数据
                    layer.confirm('请选择你的课程调整类型：', {
                        time: 20000, //20s后自动关闭
                        area: '35%',
                        btnAlign: 'c', //按钮居中对齐
                        btn: ['课程调整', '时间调整', '教室调整', '教室调整'],
                        btn1: (index, layero) =>{
                            layer.open({
                                type:2,
                                title:'选择调整课程',
                                area: ['90%','90%'],
                                content:'./changeTable.html',
                                cancel:() =>{ //关闭弹出窗口后触发此回调
                                    let changeData = JSON.parse(sessionStorage.getItem('changeData'));
                                    if(changeData){
                                        layer.open({
                                            type:2,
                                            title:'调课申请',
                                            area: ['60%','85%'],
                                            content:'./add/courseChange.html'
                                        });
                                    }else layer.msg('你以取消申请！');
                                }
                            });
                            layer.close(index);
                            // return false; 阻止按钮关闭
                        },
                        btn2: (index, layero) =>{
                            layer.open({
                                type:2,
                                title:'调课申请',
                                area: ['30%','60%'],
                                content:'./add/timeChange.html',
                                cancel:() =>{
                                   layer.msg('你以取消申请！');
                                }
                            });
                            layer.close(index);
                        },
                        btn3: (index, layero) =>{
                            layer.close(index);
                        },
                        btn4: (index, layero) =>{
                            layer.close(index);
                        }
                    });
                }
            }
        })
    });
    //进行模糊查询 (用户名和类型)
    form.on('submit(search)', function(data){
        utils.tableSelect(table, 'userTable', data, '/Class/table/getAllTables');
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
``});